package array

func generateMatrix(n int) [][]int {
	l := 0
	r := n - 1
	t := 0
	b := n - 1
	num := 1
	tar := n * n
	mat := make([][]int, n)
	for i := 0; i < n; i++ {
		mat[i] = make([]int, n)
	}
	for num <= tar {
		for i := l; i <= r; i++ {
			mat[t][i] = num
			num++
		}
		t++
		for i := t; i <= b; i++ {
			mat[i][r] = num
			num++
		}
		r--
		for i := r; i >= l; i-- {
			mat[b][i] = num
			num++
		}
		b--
		for i := b; i >= t; i-- {
			mat[i][l] = num
			num++
		}
		l++
	}
	return mat
}
